<template>
	<view class="">
		<u-navbar title="客服" is-fixed
			background="url('https://www.yplm888.com//uploads/20220803/7ed305f9a93ba50b049a7725662b18cc.png') no-repeat">
		</u-navbar>
		<scroll-view class="" :scroll-y="true" :style="'height:' + scrollH+'rpx;'" refresher-enabled="true"
			:refresher-triggered="triggered" :refresher-threshold="50" @refresherpulling="onPulling"
			@refresherrefresh="onRefresh" @refresherrestore="onRestore" @refresherabort="onAbort">
			<view class="chat-list" v-for="item in chatList" :key="item.id" @click="toChat(item)">
				<image class="iamge" :src="item.avatar_image" mode=""></image>
				<view class="text">
					{{item.name}}
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				chatList: [],
				baseUrl: this.$u.http.config.baseUrl,
				config: "",
				scrollH: 0,
				triggered: false,
			}
		},
		onLoad() {
			this._freshing = false;
			this.getChatList()
			this.getConfig()
		},
		onReady() {
			let that = this;
			uni.getSystemInfo({
				success: function(res) {
					that.scrollH = res.windowHeight * 750 / res.windowWidth;
					// #ifdef MP-WEIXIN
					that.scrollH = res.windowHeight * 750 / res.windowWidth - parseInt(res.safeArea.top) - 200;
					// #endif
				}
			});
		},
		methods: {
			onPulling(e) {
				this.triggered = true;
			},
			async onRefresh() {
				if (this._freshing) return;
				this._freshing = true;
				this.page = 1;
				await setTimeout(() => {
					this.triggered = false;
					this._freshing = false;
					this.dataList = []
					this.initData();
				}, 1000);
				uni.showToast({
					title: '刷新成功',
					duration: 1000
				})
			},
			onRestore() {
				this.triggered = false; // 需要重置
				console.log("onRestore");
			},
			onAbort() {
				console.log("onAbort");
			},
			getChatList() {
				this.$u.api.getChatList().then(res => {
					this.chatList = res.data
				})
			},
			getConfig() {
				this.$u.api.getChatConfig().then(res => {
					this.config = res.data.corpId
				})
			},
			toChat(data) {
				try {
					wx.openCustomerServiceChat({
						extInfo: {
							url: data.url //客服ID
						},
						corpId: this.config, //企业微信ID
						success(res) {}
					})
				} catch (error) {}
			}
		}
	}
</script>

<style lang="scss">
	.chat-list {
		height: 150rpx;
		margin: 10rpx 40rpx;
		// border-bottom: 1rpx solid #ccc;
		display: flex;
		border-radius: 20rpx;
		box-shadow: 0 0px 12px 0 rgba(0, 0, 0, 0.1);

		.iamge {
			margin: 10rpx;
			height: 130rpx;
			width: 130rpx;
			border-radius: 20rpx;
		}

		.text {
			line-height: 150rpx;
			margin-left: 30rpx;
		}
	}
</style>
